#!/usr/bin/env python
# -*- coding:utf8 -*-

"""
@version:
@author: lh
@license: Apache Licence
@contact: liuhuan0672@gmail.com
@site:
@software: PyCharm
@file: 0003.py
@time: 2016/1/20 13:34

第 0003 题：将 0001 题生成的 200 个激活码（或者优惠券）保存到 Redis 非关系型数据库中。
"""
import uuid
import redis


def generate_key():
    key_list = []
    for i in range(200):
        uuid_key = uuid.uuid3(uuid.NAMESPACE_DNS, str(uuid.uuid1()))
        key_list.append(str(uuid_key).replace('-', ''))
    return key_list


def redis_pool_init():
    global re
    redis_pool = redis.ConnectionPool(host='localhost', port=6379, db=0)
    re = redis.Redis(connection_pool=redis_pool)


def write_to_redis(key_list):
    for i in range(200):
        re.sadd('u_key', key_list[i])


def get_keys_from_redis():
    return re.smembers('u_key')


if __name__ == '__main__':
    redis_pool_init()
    # re.delete('u_key')
    write_to_redis(generate_key())
    for i in get_keys_from_redis():
        print(i)
